package com.niit.order.service.impl;


import com.niit.order.mapper.OrderMapper;
import com.niit.order.pojo.Order;
import com.niit.order.pojo.User;
import com.niit.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 服务层实现类
 * @author yaojianfeng
 */
@Service("orderService")
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        if (order != null) {
            //2. 利用RestTemplate发起Http请求,查询用户
            //该RestTemplate实例没有实现负载均衡,不会从注册中心拉取服务列表怎么
            RestTemplate restTemplate = new RestTemplate();
            //2.1 url路径
            String url = "http://localhost:9020/user/"+order.getUserId();
            //2.2 发送http请求,实现远程调用
            User user = restTemplate.getForObject(url, User.class);
            //3.封装user信息到order中
            order.setUser(user);
        }
        // 4.返回
        return order;
    }
}
